package com.ruoyi.monitorTools.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.monitorTools.domain.A2ROfEFIntrusion;
import com.ruoyi.monitorTools.domain.ElectronicFence;
import com.ruoyi.monitorTools.domain.RegionalInfo;
import com.ruoyi.monitorTools.mapper.ElectronicFenceMapper;
import com.ruoyi.monitorTools.service.ElectronicFenceService;
import com.ruoyi.monitorTools.service.RegionalInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【a2_electronic fence(电子围栏列表)】的数据库操作Service实现
* @createDate 2025-02-11 13:34:24
*/
@Service
public class ElectronicFenceServiceImpl extends ServiceImpl<ElectronicFenceMapper, ElectronicFence>
    implements ElectronicFenceService {

    @Autowired
    private RegionalInfoService regionalInfoService;

    @Autowired
    private ElectronicFenceMapper electronicFenceMapper;

    @Override
    @Transactional
    public Integer insertOne(ElectronicFence electronicFence) {
        //先保存区域
        // 选择其他区域作为电子围栏时，不需要添加区域， 直接保存
        if(StringUtils.isNull(electronicFence.getRegionId())) {
            //设置区域范围
            RegionalInfo regionalInfo = new RegionalInfo();
            regionalInfo.setRegionGeojson(electronicFence.getRegionGeojson());
            regionalInfo.setRegionName(electronicFence.getRegionName());
            Long regionalId = regionalInfoService.addRegional(regionalInfo);
            electronicFence.setRegionId(regionalId);
        }
        electronicFence.setCreateTime(new Date());
        int resultCount = electronicFenceMapper.insert(electronicFence);
        return resultCount;
    }

    @Override
    public AjaxResult updateByTaskIdAndRegionalId(ElectronicFence electronicFence) {
        electronicFenceMapper.updateById(electronicFence);
        return AjaxResult.success();
    }

    @Override
    public ElectronicFence selectEleById(Long id) {
        ElectronicFence electronicFence = electronicFenceMapper.selectById(id);
        RegionalInfo regionalInfo = regionalInfoService.getById(electronicFence.getRegionId());
        electronicFence.setRegionGeojson(regionalInfo.getRegionGeojson());
        electronicFence.setRegionName(regionalInfo.getRegionName());
        return electronicFence;
    }

    @Override
    public List<ElectronicFence> listByPage(String regionalName, Integer enableStatus) {
        return electronicFenceMapper.listByPage(regionalName, enableStatus);
    }

    @Override
    public List<RegionalInfo> selectOtherList() {
        QueryWrapper<ElectronicFence> eleQueryWrap = new QueryWrapper<>();
        eleQueryWrap.select("region_id");
        List<ElectronicFence> electronicFences = electronicFenceMapper.selectList(eleQueryWrap);
        List<Long> collect = electronicFences.stream().map(ele -> ele.getRegionId()).collect(Collectors.toList());
        QueryWrapper<RegionalInfo> regionWrapper = new QueryWrapper<>();
        regionWrapper.select("id", "region_name")
                .notIn(CollectionUtil.isNotEmpty(collect), "id", collect);
        List<RegionalInfo> list = regionalInfoService.list(regionWrapper);
        return list;
    }

    @Override
    public Integer updateEnableStatusById(Long id, Integer enableStatus) {
        UpdateWrapper<ElectronicFence> electronicFenceQueryWrapper = new UpdateWrapper<>();
        electronicFenceQueryWrapper.eq("task_id", id);
        electronicFenceQueryWrapper.set("enable_status", enableStatus);
        int update = electronicFenceMapper.update(null, electronicFenceQueryWrapper);
        return update;
    }

    @Override
    public AjaxResult batchUpdateEnableStatus(List<Integer> idList, Integer enableStatus) {
        try {
            UpdateWrapper<ElectronicFence> wrapper = new UpdateWrapper<>();
            wrapper.in("task_id", idList);
            wrapper.set("enable_status", enableStatus);
            electronicFenceMapper.update(null, wrapper);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("修改失败",  e);
            return AjaxResult.error();
        }
    }

    @Override
    public List<ElectronicFence> isBreakOrOut(double lon, double lat, Date baseTime) {
        return electronicFenceMapper.isContain(lon, lat, baseTime);
    }

    @Override
    public List<A2ROfEFIntrusion> selectBehavior(Long taskId, Integer behavior, String targetName) {
        return electronicFenceMapper.selectBehavior(taskId, behavior, targetName);
    }


}




